

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>综合实例</title>
    <style>
        *{
            padding: 0;
            margin: 0;
            /* 设置盒模型  怪异盒模型  设置的宽高就是盒子的宽高= content的宽高 +2* border +2* padding   */
            box-sizing: border-box;
        }
        .goods{
            width: 1000px;
            margin: 100px auto;
        }
        .tab{
            display: flex;
            margin-left: 40px;
        }
        .tab a {
            padding: 10px 20px ;
            background: #f6f6f6;
            margin-right: 20px;
            color: #666;
            cursor: pointer;
            text-decoration: none;
        }
        .tab a:active,
        .tab a:focus {
            background-color: red;
        }


        .list {
            width: 1000px;
            /* height: 1000px; */
            margin: 50px auto;
            /* 弹性布局  让元素在一行显示 */
            display: flex;
             /* 当前行放不下的时候进行换行 */
            flex-wrap: wrap;
            background-color: bisque;
        }
        .item{
            width: 240px;
            padding: 20px 30px;
            /* 动画  过渡动画 */
            transition: all .5s;
            margin-bottom: 20px;
            margin-left: 10px;
        }
        .item:hover{
            box-shadow: 0  0 5px rgba(0, 0, 0, 0.2);
        }
        .item img{
            width: 100%;
        }
        .item .name{
            font-size: 18px;
            color: #666;
            margin-bottom: 10px;
        }
        .item .price{
            font-size: 22px;
            color: firebrick;
        }
    
        /* : 伪类选择器 ：css3伪类  ::css3伪元素    用于创建元素的内容 */
        .item .price::before{
            content: "￥";
            font-size: 14px;
        }

    </style>
</head>
<body>
    <div class="goods">
        <div class="tab">
            <a  data-index="1" href="javascript:;">0-100元</a>
            <a  data-index="2" href="javascript:;">100-300元</a>
            <a  data-index="3" href="javascript:;">300元以上</a>
            <a href="javascript:;">全部区间</a>
        </div>
    </div>

    <div class="list">
         
        <!-- <div class="item">
            <img src="https://yanxuan-item.nosdn.127.net/84a59ff9c58a77032564e61f716846d6.jpg" alt="">
            <p class="name">称心如意手摇咖啡磨豆机咖啡豆研磨机</p>
            <p class="price">289.00</p>
        </div>
        -->
    </div>



    <script>
        // 被渲染的数据 json 对象   数组
        // 1. 定义后台返回的数据-json  
    const goodsList = [
        {
            id: '4001172',
            name: '称心如意手摇咖啡磨豆机咖啡豆研磨机',
            price: '289.00',
            picture: 'https://yanxuan-item.nosdn.127.net/84a59ff9c58a77032564e61f716846d6.jpg',
        },
        {
            id: '4001594',
            name: '日式黑陶功夫茶组双侧把茶具礼盒装',
            price: '288.00',
            picture: 'https://yanxuan-item.nosdn.127.net/3346b7b92f9563c7a7e24c7ead883f18.jpg',
        },
        {
            id: '4001009',
            name: '竹制干泡茶盘正方形沥水茶台品茶盘',
            price: '109.00',
            picture: 'https://yanxuan-item.nosdn.127.net/2d942d6bc94f1e230763e1a5a3b379e1.png',
        },
        {
            id: '4001874',
            name: '古法温酒汝瓷酒具套装白酒杯莲花温酒器',
            price: '488.00',
            picture: 'https://yanxuan-item.nosdn.127.net/44e51622800e4fceb6bee8e616da85fd.png',
        },
        {
            id: '4001649',
            name: '大师监制龙泉青瓷茶叶罐',
            price: '139.00',
            picture: 'https://yanxuan-item.nosdn.127.net/4356c9fc150753775fe56b465314f1eb.png',
        },
        {
            id: '3997185',
            name: '与众不同的口感汝瓷白酒杯套组1壶4杯',
            price: '108.00',
            picture: 'https://yanxuan-item.nosdn.127.net/8e21c794dfd3a4e8573273ddae50bce2.jpg',
        },
        {
            id: '3997403',
            name: '手工吹制更厚实白酒杯壶套装6壶6杯',
            price: '99.00',
            picture: 'https://yanxuan-item.nosdn.127.net/af2371a65f60bce152a61fc22745ff3f.jpg',
        },
        {
            id: '3998274',
            name: '德国百年工艺高端水晶玻璃红酒杯2支装',
            price: '139.00',
            picture: 'https://yanxuan-item.nosdn.127.net/8896b897b3ec6639bbd1134d66b9715c.jpg',
        },
    ]

        // 2:渲染列表数据  遍历数组数据  for  for in  forEach(回调函数)  map(回调函数)   渲染函数 -渲染列表数据 遍历的数组
       function render(data) {
            let list = data.map(function (item,index) {
                // console.log(item,index);
                // 对象的解构赋值
                let { name,price,picture} = item;
                console.log(name);
                console.log(price);
                console.log(picture);
                // 动态创建html结构
                return  `
                    <div class="item">
                        <img src="${picture}" alt="">
                        <p class="name">${name}</p>
                        <p class="price">${price}</p>
                    </div>
                `
            })

            document.querySelector(".list").innerHTML = list.join("")
       }
        
       render(goodsList)
       

       // 3:价格筛选功能    数组的方法 过滤方法 filter(回调函数)
        // 绑定事件  事件委托

        document.querySelector(".tab").addEventListener("click",function (event) {
            // 事件委托
            // console.log(event.target);
            const {nodeName,dataset} = event.target
            // console.log(nodeName);
            // console.log(dataset)
            if (nodeName === "A") {
                const { index } = dataset
                // console.log(index);
                let arr = goodsList
                // 0-100
                if (index == "1") {
                    arr = goodsList.filter(function (item) {
                        return item.price>=0 && item.price<=100
                    })
                } else if(index == "2") {
                    arr = goodsList.filter(function (item) {
                        return item.price>=100 && item.price<=300
                    })
                }else if(index == "3"){
                    arr = goodsList.filter(function (item) {
                        return item.price>=300
                    })
                }

                console.log(arr);
                
                render(arr)
            }
            
            
        })





    </script>
</body>
</html>